package com.woniu.clientServer.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.clientServer.dto.ApprovalDto;
import com.woniu.clientServer.feign.UumsFeign;
import com.woniu.clientServer.feign.WorkFeign;
import com.woniu.clientServer.mapper.CustomerApplicationMapper;
import com.woniu.clientServer.pojo.ApprovalRecord;
import com.woniu.clientServer.pojo.Customer;
import com.woniu.clientServer.pojo.CustomerApplication;
import com.woniu.clientServer.service.ApprovalRecordService;
import com.woniu.clientServer.mapper.ApprovalRecordMapper;
import com.woniu.clientServer.service.CustomerApplicationService;
import com.woniu.clientServer.service.CustomerService;
import com.woniu.common.utils.ResponseData;
import com.woniu.server.dto.PowerUserDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 404
* @description 针对表【approval_record(审批记录表)】的数据库操作Service实现
* @createDate 2024-11-27 16:12:54
*/
@Service
public class ApprovalRecordServiceImpl extends ServiceImpl<ApprovalRecordMapper, ApprovalRecord>
    implements ApprovalRecordService{
    @Autowired
    private UumsFeign uumsFeign;
    @Autowired
    private WorkFeign workFeign;
    @Autowired
    private CustomerApplicationMapper applicationMapper;
    @Autowired
    private CustomerApplicationService applicationService;
    @Autowired
    private ApprovalRecordMapper approvalRecordMapper;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ApprovalRecordService approvalRecordService;
    /**
     * 审批通过流程
     * @param
     * @return
     */
    @Override
    public ResponseData<?> doApproval(ApprovalDto approvalDto) {
        ApprovalRecord one = lambdaQuery().eq(ApprovalRecord::getApprovalRecordCode,approvalDto.getApprovalRecordCode()).one();
        PowerUserDto powerUserDto = uumsFeign.queryByCode(one.getApprovalCode());
        workFeign.completeTask(one.getProcessInstanceId(), one.getApprovalResult(), powerUserDto.getRoleName());
        CustomerApplication customerApplication = applicationMapper.queryAppByCode(one.getApplicationCustomerCode());
        ApprovalRecord approvalRecord = new ApprovalRecord();
        if ("总监".equals(powerUserDto.getRoleName())) {
                //是总监，同意下一步就是经理
                customerApplication.setStatus(2);
                applicationService.updateStatus(customerApplication);
                one.setApprovalResult(2);
                approvalRecordService.updateResult(one);
                CustomerApplication customerApplication1 = new CustomerApplication();
                BeanUtils.copyProperties(customerApplication1, customerApplication);
                customerApplication1.setStatus(2);
                approvalRecord.setApprovalRecordCode(String.valueOf(IdUtil.getSnowflake(1,1).nextId()));
                approvalRecord.setApplicationCustomerCode(one.getApplicationCustomerCode());
                approvalRecord.setApprovalCode("5");
                approvalRecord.setApprovalName("xxy");
                approvalRecord.setStatus(2);
                approvalRecord.setApprovalResult(1);
                approvalRecord.setProcessInstanceId(one.getProcessInstanceId());
                approvalRecord.setCreatedBy(one.getCreatedBy());
                approvalRecordMapper.save(approvalRecord);
                return  new ResponseData<>().ok();

        } else {
            //经理
            //申请表完成
            customerApplication.setStatus(3);
            applicationService.updateStatus(customerApplication);
            //更新申请流程表
            one.setApprovalResult(2);
            updateById(one);
            //客户写入客户表
            Customer customer = new Customer();
            customer.setCustomerId(customerApplication.getCustomerId());
            customer.setCustomerName(customerApplication.getCustomerName());
            customer.setCompanyName(customerApplication.getCompanyName());
            customer.setBusinessNature(customerApplication.getBusinessNature());
            customer.setIndustry(customerApplication.getIndustry());
            customer.setCustomerLevel(customerApplication.getCustomerLevel());
            customer.setCustomerCategory(customerApplication.getCustomerCategory());
            customer.setRegisteredCapital(customerApplication.getRegisteredCapital());
            customer.setStaffSize(customerApplication.getStaffSize());
            customer.setRegion(customerApplication.getRegion());
            customer.setCompanyAddress(customerApplication.getCompanyAddress());
            customer.setRegistrationAddress(customerApplication.getRegistrationAddress());
            customer.setCreditCode(customerApplication.getCreditCode());
            customer.setRemarks(customerApplication.getRemarks());
            customer.setLegalRepresentative(customerApplication.getLegalRepresentative());
            customer.setCompanyPhone(customerApplication.getCompanyPhone());
            customer.setContactPerson(customerApplication.getContactPerson());
            customer.setContactPhone(customerApplication.getContactPhone());
            customer.setEmail(customerApplication.getEmail());
            customer.setBankId(customerApplication.getBankId());
            customer.setBankAccount(customerApplication.getBankAccount());
            customer.setAccountName(customerApplication.getAccountName());
            customer.setDeliveryAddress(customerApplication.getDeliveryAddress());
            customer.setCode(String.valueOf(IdUtil.getSnowflake(1,1).nextId()));
            customerService.save(customer);
            return new ResponseData<>().ok();
        }
    }

    @Override
    public void updateResult(ApprovalRecord one) {
        lambdaUpdate().eq(ApprovalRecord::getApprovalRecordCode,one.getApprovalRecordCode())
                .set(ApprovalRecord::getApprovalResult, one.getApprovalResult())
                .update();
    }

    @Override
    public ResponseData<?> rejectReason(ApprovalDto approvalDto) {
        ApprovalRecord one = lambdaQuery().eq(ApprovalRecord::getApprovalRecordCode,approvalDto.getApprovalRecordCode()).one();
        PowerUserDto powerUserDto = uumsFeign.queryByCode(one.getApprovalCode());
        one.setApprovalComments(approvalDto.getRejectReason());
        one.setApprovalResult(0);
        updateById(one);
        workFeign.completeTask(one.getProcessInstanceId(), one.getApprovalResult(), powerUserDto.getRoleName());
        CustomerApplication customerApplication = applicationMapper.queryAppByCode(one.getApplicationCustomerCode());
        if ("总监".equals(powerUserDto.getRoleName())) {
            //如果是总监，直接更改审批状态,流程到此结束
            customerApplication.setStatus(0);
            applicationService.updateStatus(customerApplication);
        }else {
            //经理,驳回申请，重新生成给总监
            customerApplication.setStatus(1);
            applicationService.updateStatus(customerApplication);
            ApprovalRecord approvalRecord = new ApprovalRecord();
            approvalRecord.setApprovalName("zf");
            approvalRecord.setApprovalCode("1");
            approvalRecord.setApprovalRecordCode(String.valueOf(IdUtil.getSnowflake(1,1).nextId()));
            approvalRecord.setApplicationCustomerCode(one.getApplicationCustomerCode());
            approvalRecord.setStatus(1);
            approvalRecord.setApprovalResult(1);
            approvalRecord.setProcessInstanceId(one.getProcessInstanceId());
            approvalRecordMapper.save(approvalRecord);
        }
        return new ResponseData<>().ok();
    }
}




